package com.zjuh.qss.web.action.admin;

import java.util.List;
import java.util.Map;

import wint.help.biz.result.Result;
import wint.lang.convert.ConvertUtil;
import wint.lang.utils.CollectionUtil;
import wint.lang.utils.StringUtil;
import wint.mvc.flow.FlowData;
import wint.mvc.form.Form;
import wint.mvc.form.fileupload.UploadFile;
import wint.mvc.module.annotations.Action;
import wint.mvc.parameters.Parameters;
import wint.mvc.template.Context;

import com.zjuh.qss.biz.ao.TaokeQueryAO;
import com.zjuh.qss.biz.ao.TopicAO;
import com.zjuh.qss.biz.dao.query.TopicItemQuery;
import com.zjuh.qss.biz.dao.query.TopicQuery;
import com.zjuh.qss.biz.domain.TopicDO;
import com.zjuh.qss.web.common.BaseAction;

public class Topic extends BaseAction {

	private TopicAO topicAO;
	
	private TaokeQueryAO taokeQueryAO;
	
	public void removeItem(FlowData flowData, Context context) {
		Parameters parameters = flowData.getParameters();
		long id = parameters.getLong("id");
		long topicId = parameters.getLong("topicId");
		Result result = topicAO.removeItemFromTopic(id);
		if (result.isSuccess()) {
			flowData.redirectTo("adminModule", "topic/detail").args(topicId);
		} else {
			this.handleError(result, flowData, context);
		}
	}
	
	public void doAddItemsToTopic(FlowData flowData, Context context) {
		Parameters parameters = flowData.getParameters();
		String itemIds = parameters.getString("itemIds");
		long topicId = parameters.getLong("topicId");
		List<String> strItemIds = StringUtil.splitTrim(itemIds, ",");
		List<Long> ids = CollectionUtil.newArrayList();
		for (String s : strItemIds) {
			ids.add(ConvertUtil.toLong(s, 0));
		}
		Result result = topicAO.addItemsToTopic(topicId, ids);
		if (result.isSuccess()) {
			flowData.redirectTo("adminModule", "topic/detail").args(topicId);
		} else {
			this.handleError(result, flowData, context);
		}
	}
	
	public void addItemToTopic(FlowData flowData, Context context) {
		Parameters parameters = flowData.getParameters();
		long topicId = parameters.getLong("topicId");
		long itemId = parameters.getLong("itemId");
		Result result = topicAO.addItemToTopic(topicId, itemId);
		if (result.isSuccess()) {
			flowData.redirectTo("adminModule", "topic/detail").args(topicId);
		} else {
			this.handleError(result, flowData, context);
		}
	}
	
	public void search(FlowData flowData, Context context) {
		String q = flowData.getParameters().getString("q");
		long cid = flowData.getParameters().getLong("cid", -1);
		double commission = flowData.getParameters().getDouble("commission", 0);
		int page = flowData.getParameters().getInt("page", 1);
		TopicItemQuery topicItemQuery = new TopicItemQuery();
		topicItemQuery.setCid(cid);
		topicItemQuery.setQ(q);
		topicItemQuery.setPageSize(100);
		topicItemQuery.setPageNo(page);
		topicItemQuery.setMinCommission(commission);
		flowData.setTarget("admin/topic/addItem");
		Result result = topicAO.searchTopicItems(topicItemQuery);
		this.handleResult(result, flowData, context);
	}
	
	public void searchByItemId(FlowData flowData, Context context) {
		long itemId = flowData.getParameters().getLong("itemId");
		flowData.setTarget("admin/topic/addItem");
		Result result = taokeQueryAO.queryByItemId(itemId);
		this.handleResult(result, flowData, context);
	}
	
	
	@Action(defaultTarget="admin/topic/create")
	public void doCreate(FlowData flowData, Context context) {
		Form form = flowData.getForm("topic.create");
		TopicDO topicDO = new TopicDO();
		if (!form.apply(topicDO)) {
			return;
		}
		
		Map<String, UploadFile> uploadFiles = flowData.getUploadFiles();
		
		Result result = topicAO.createTopic(topicDO, uploadFiles);
		if (result.isSuccess()) {
			flowData.redirectTo("adminModule", "topic/list");
		} else {
			this.handleError(result, flowData, context);
		}
	}
	
	public void list(FlowData flowData, Context context) {
		int page = flowData.getParameters().getInt("page", 1);
		TopicQuery topicQuery = new TopicQuery();
		topicQuery.setPageNo(page);
		topicQuery.setPageSize(40);
		Result result = topicAO.viewTopicListForAdmin(topicQuery);
		this.handleResult(result, flowData, context);
	}
	
	public void detail(FlowData flowData, Context context, long id, int page) {
		Result result = topicAO.viewTopicDetailForAdmin(id, page);
		this.handleResult(result, flowData, context);
	}
	
	public void delete(FlowData flowData, Context context, long id) {
		Result result = topicAO.deleteTopicDetailForAdmin(id);
		if (result.isSuccess()) {
			flowData.redirectTo("adminModule", "topic/list");
		} else {
			this.handleError(result, flowData, context);
		}
	}
	
	
	
	public void modifyImage(FlowData flowData, Context context, long id) {
		Result result = topicAO.viewTopicModifyForAdmin(id);
		if (result.isSuccess()) {
			this.result2Context(result, context);
		} else {
			this.handleError(result, flowData, context);
		}
	}
	
	@Action(defaultTarget="admin/topic/modifyImage")
	public void doModifyImage(FlowData flowData, Context context) {
		long id = flowData.getParameters().getLong("id");
		Map<String, UploadFile> uploadFiles = flowData.getUploadFiles();
		Result result = topicAO.modifyTopicImage(id, uploadFiles);
		if (result.isSuccess()) {
			flowData.redirectTo("adminModule", "topic/list");
		} else {
			this.handleError(result, flowData, context);
		}
	}
	
	
	
	
	public void modify(FlowData flowData, Context context, long id) {
		Result result = topicAO.viewTopicModifyForAdmin(id);
		if (result.isSuccess()) {
			hold(flowData, result, "topic.modify", "topic");
			this.result2Context(result, context);
		} else {
			this.handleError(result, flowData, context);
		}
	}
	
	@Action(defaultTarget="admin/topic/modify")
	public void doModify(FlowData flowData, Context context) {
		Form form = flowData.getForm("topic.modify");
		TopicDO topicDO = new TopicDO();
		if (!form.apply(topicDO)) {
			return;
		}
		Result result = topicAO.modifyTopicForAdmin(topicDO);
		if (result.isSuccess()) {
			flowData.redirectTo("adminModule", "topic/detail").args(topicDO.getId());
		} else {
			this.handleError(result, flowData, context);
		}
		
	}

	public void setTopicAO(TopicAO topicAO) {
		this.topicAO = topicAO;
	}


	public void setTaokeQueryAO(TaokeQueryAO taokeQueryAO) {
		this.taokeQueryAO = taokeQueryAO;
	}
	
}
